/*
 * @Author: LiuYan
 * @Date: 2022-08-06 09:41:14
 * @LastEditors: 刘岩 15136056318@163.com
 * @LastEditTime: 2023-07-31 19:33:36
 * @Description:
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import config from '../config'
import { userStore } from '../store/user-store'
const { APP } = config
const routes = [
  {
    path: '/',
    redirect: '/layout',
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../view/layout'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../view/login'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})
router.beforeEach((to, from, next) => {
  const user = userStore()
  if (to.meta.title) {
    document.title = to.meta.title
  } else {
    document.title = APP.title
  }
  if (APP.isLogin) {
    if (to.fullPath === '/login') {
      next()
    } else {
      if (user.token && user.token.length) {
        next()
      } else {
        next('/login')
      }
    }
  } else {
    next()
  }
})
export default router
